//
//  VCOfSeacher.m
//  XYFMApp
//
//  Created by 岁变 on 16/9/10.
//  Copyright © 2016年 岁变. All rights reserved.
//

#import "VCOfSeacher.h"
#import "XYMacro.h"

@interface VCOfSeacher () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UITextField *searchField;
@property (nonatomic, strong) UIButton *searchButton;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSArray *mArr;


@end

@implementation VCOfSeacher

- (void)viewDidLoad {
    
    _mArr = @[@"盗墓笔记", @"鬼吹灯", @"黑暗坡食人树", @"出牌", @"三体", @"哏小姐驾到", @"我了个槽", @"好妹妹坏坏说", @"闻所未闻", @"娱乐扒扒扒", @"拥你入睡", @"在异乡", @"音乐日记"];
    [self configNavigationController];
    [self configViewController];
}




- (void)configNavigationController {
    self.title = @"🔍 搜索";
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[UIColor whiteColor]};
    UIButton *goBackButton = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackButton.frame = CGRectMake(0, 0, 30, 30);
    [goBackButton setImage:[UIImage imageNamed:@"go_back"] forState:UIControlStateNormal];
    [goBackButton addTarget:self action:@selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:goBackButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
}


- (void)configViewController {
    _backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    [self.view addSubview:_backView];
    _backView.backgroundColor = BACKGROUNDCOLOR;
    
    [self createSearchBar];
    
    [self createHotSearch];
    

}

- (void)createSearchBar {
    UIView *searchBackView = [[UIView alloc] initWithFrame:CGRectMake(30, 20, SCREEN_WIDTH - 120, 40)];
    searchBackView.backgroundColor = [UIColor colorWithRed:0.32 green:0.33 blue:0.40 alpha:1.00];
    searchBackView.layer.borderWidth = 1;
    searchBackView.layer.borderColor = [UIColor colorWithRed:0.42 green:0.42 blue:0.49 alpha:1.00].CGColor;
    searchBackView.layer.cornerRadius = 5;
    [_backView addSubview:searchBackView];
    
    _searchField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, searchBackView.frame.size.width - 30, 40)];
    
    
    _searchField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"搜索主播, 栏目, 节目" attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRed:0.60 green:0.63 blue:0.70 alpha:1.00]}];
    _searchField.backgroundColor = [UIColor colorWithRed:0.32 green:0.33 blue:0.40 alpha:1.00];
    _searchField.tintColor = [UIColor colorWithRed:0.60 green:0.63 blue:0.70 alpha:1.00];
    [searchBackView addSubview:_searchField];
    
    _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _searchButton.backgroundColor = [UIColor colorWithRed:1.00 green:0.27 blue:0.12 alpha:1.00];
    [_searchButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _searchButton.frame = CGRectMake(SCREEN_WIDTH - 80, 20, 50, 40);
    [_searchButton setTitle:@"搜索" forState:UIControlStateNormal];
    [_searchButton addTarget:self action:@selector(clickSearch:) forControlEvents:UIControlEventTouchUpInside];
    _searchButton.layer.cornerRadius = 5;
    [_backView addSubview:_searchButton];
    
}

- (void)createHotSearch {
    UIView *lineViewOne = [[UIView alloc] initWithFrame:CGRectMake(10, 150, SCREEN_WIDTH - 100, 2)];
    lineViewOne.backgroundColor = [UIColor colorWithRed:0.51 green:0.49 blue:0.57 alpha:1.00];
    [_backView addSubview:lineViewOne];
    
    UIView *lineViewTwo = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 2, 500)];
    lineViewTwo.backgroundColor = [UIColor colorWithRed:0.51 green:0.49 blue:0.57 alpha:1.00];
    [_backView addSubview:lineViewTwo];
    
    UIView *pointView = [[UIView alloc] initWithFrame:CGRectMake(43, 143, 16, 16)];
    pointView.clipsToBounds = YES;
    pointView.backgroundColor = [UIColor whiteColor];
    pointView.layer.cornerRadius = 8;
    [_backView addSubview:pointView];
    
    UILabel *hotSearchLable = [[UILabel alloc] initWithFrame:CGRectMake(70, 120, 200, 30)];
    hotSearchLable.text = @"热门搜索";
    hotSearchLable.textColor = [UIColor redColor];
    [_backView addSubview:hotSearchLable];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 15;
    layout.minimumInteritemSpacing = 20;
    layout.scrollDirection = UICollectionViewScrollDirectionVertical;
    
    _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(70, 165, SCREEN_WIDTH - 100, 300) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = BACKGROUNDCOLOR;
    _collectionView.showsVerticalScrollIndicator = NO;
    [_backView addSubview:_collectionView];
    [_collectionView registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"SearchCollectionViewCell"];
    
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return _mArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SearchCollectionViewCell" forIndexPath:indexPath];
    cell.contentView.backgroundColor = BACKGROUNDCOLOR;
    cell.titleLabel.text = [_mArr objectAtIndex:indexPath.item];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *str = [_mArr objectAtIndex:indexPath.row];
    
    CGRect newRect = [str boundingRectWithSize:CGSizeMake(0, 40) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17]} context:nil];
    CGSize newSize = CGSizeMake(newRect.size.width + 15, 40);
    return newSize;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = [_mArr objectAtIndex:indexPath.row];
    _searchField.text = str;
    
}



- (void)clickSearch:(UIButton *)button {
    
    if (_searchField.text.length != 0) {
        VCOfSearchConent *nextPage = [[VCOfSearchConent alloc] init];
        nextPage.keyword = _searchField.text;
        [self.navigationController pushViewController:nextPage animated:YES];

        }
}



- (void)goBack:(UIButton *)button {
    
    [self.navigationController popViewControllerAnimated:YES];
    self.tabBarController.tabBar.hidden = NO;
    
    
}


- (void)viewDidAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
